package com.example.demo.controller;

import com.example.demo.service.UserService;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-02-24
 * Time: 15:14
 */
@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {

    @Autowired
    private UserService userService;

    /**
     * 正常操作, 没有抛出异常
     */
    @Transactional
    @RequestMapping("/registry")
    public String registry(String userName,String password){
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        return "注册成功";
    }

    /**
     * 程序报错，异常捕获
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r3")
    public String r3(String userName,String password){
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        try {
            int a= 10/0;
        }catch (Exception e) {
            log.info("程序出错");
        }
        return "注册成功";
    }

    /**
     *程序报错, 重新抛出异常
     *事务回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public String r4(String userName,String password){
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        try {
            int a= 10/0;
        }catch (Exception e) {
            log.info("程序出错");
            throw  e;
        }
        return "注册成功";
    }


    @Transactional
    @RequestMapping("/r5")
    public String r5(String userName,String password){
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        try {
            int a= 10/0;
        }catch (Exception e) {
            log.info("程序出错");
            //当前事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    @Transactional
    @RequestMapping("/r6")
    public String r6(String userName,String password) throws IOException {
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        try {
            int a= 10/0;
        }catch (Exception e) {
            log.info("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }


    @RequestMapping("/r7")
    @Transactional(rollbackFor = {Exception.class,Error.class})
    public String r7(String userName,String password) throws IOException {
        Integer result = userService.insertUser(userName,password);
        log.info("用户插入成功");
        try {
            int a= 10/0;
        }catch (Exception e) {
            log.info("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }
}
